package com.geostar.mina.core;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import com.geostar.mina.model.ProcessModel;
import com.geostar.mina.utils.CreateID;
import com.geostar.mina.utils.DateUtils;

/**
 * 文件名:ProcessExe.java
 * 功能: 查找指定进程状态
 * 目前支持方法
 * --------------------------------------
 * 初始版本 
 * 作者: yesong
 * 时间: Apr 14, 2014
 */
public class ProcessExe {

	private static final String TRUE = "1";
	private static final String FALSE = "0";
	private static final String NORUN = "0";

	/**
	 * 目的: 查找指定进程状态
	 * 
	 * @param pmList
	 * @return 返回已有状态值的进程对象集合
	 * 
	 */
	public static List<ProcessModel> process(List<ProcessModel> pmList) {
		// 执行命令
		final String command = "tasklist /v";
		Runtime runtime = Runtime.getRuntime();
		List<ProcessModel> newProList = new ArrayList<ProcessModel>();
		try {
			Process process = runtime.exec(command);
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					process.getInputStream()));

			String info = null;

			while ((info = reader.readLine()) != null) {
				ProcessModel pm = getProcessInfo(pmList, info);
				if(pm != null){
					newProList.add(pm);
				}
				reader.readLine();
			}
			for (int i = 0; i < pmList.size(); i++) {
				ProcessModel pm = pmList.get(i);
				if(pm.getId() == null) { 
					pm.setId(CreateID.generate9());
					pm.setProcess_name(pm.getProcess_name());
					pm.setIp(pm.getIp());
					pm.setRecord_time(DateUtils.getLongTime());
					pm.setIsRunning(FALSE);
					pm.setRom_data(NORUN);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return newProList;
	}

	/**
	 * 目的: 判断 需要监控的进程名是否在info 字符转中
	 * 
	 * @param pmList
	 * @param info
	 * @return
	 * 
	 */
	public static ProcessModel getProcessInfo(List<ProcessModel> pmList,
			String info) {
		ProcessModel pm = null;
		for (int i = 0; i < pmList.size(); i++) {
			pm = pmList.get(i);
			if (info.indexOf(pm.getProcess_name()) > -1) {
				pm.setId(CreateID.generate9());
				pm.setProcess_name(pm.getProcess_name());
				pm.setIp(pm.getIp());
				pm.setRecord_time(DateUtils.getLongTime());
				
				String[] proArray = info2StringArray(info);
				pm.setIsRunning(TRUE);
				pm.setRom_data(proArray[4]);
				return pm;
			} else {
				pm = null;
			}
		}
		return pm;
	}

	/**
	 * 目的: 进程信息info 转换为 字符串数组 proArray[0] 进程名 proArray[1] PID proArray[4] 内存占用
	 * 
	 * @param info
	 * @return
	 * 
	 */
	public static String[] info2StringArray(String info) {
		String[] s = info.split(" ");
		int index = 0;
		for (int i = 0; i < s.length; i++) {
			if (StringUtils.isNotBlank(s[i])) {
				index++;
			}
		}

		String[] proArray = new String[index];
		int _index = 0;
		for (int i = 0; i < s.length; i++) {
			if (StringUtils.isNotBlank(s[i])) {
				proArray[_index] = s[i];
				++_index;
			}
		}
		return proArray;
	}
	
	public static void main(String[] args) {
		ProcessModel pm1 = new ProcessModel();
		pm1.setIp("127.0.0.1");
		pm1.setProcess_name("QQ");
		
		ProcessModel pm2 = new ProcessModel();
		pm2.setIp("127.0.0.1");
		pm2.setProcess_name("chrome");
		
		List<ProcessModel> pmList = new ArrayList<ProcessModel>();
		pmList.add(pm1);
		pmList.add(pm2);
		
		List<ProcessModel> new_pmList = ProcessExe.process(pmList);
		for (ProcessModel processModel : new_pmList) {
			System.out.println(processModel.getProcess_name()+"  " + processModel.getIsRunning() + "  " + processModel.getRom_data()); 
		}
	}
}
